<?php

namespace plugins\order\interfaceLib;

use cmf\controller\PluginAdminBaseController;

use think\Db;

use plugins\store\Common;

use plugins\order\model\OrderModel;

use plugins\order\model\OrderLogModel;

use plugins\order\model\OrderGoodsListModel;

use plugins\goods\model\GoodsSpecListModel;

use plugins\order\model\OrderMemberModel;

use plugins\finance\model\FinanceModel;

use plugins\finance\model\FinanceAccountsModel;





class OrderClassify

{

	//操作日志

	public function orderDeliver($order_number)

	{

		//权限检测 

		$store_id = ( new Common )->getAllowAccess();



		$return = ( new OrderModel )->where(['order_num'=>$order_number,'store_id'=>$store_id])->update(['order_status'=>3]);

		if($return){

			( new OrderLogModel )->createLog('12',$order_number);	

			return true;

		}

		return false;

	}



	//订单关闭或取消后返还库存,有sku的情况
	public function inventoryBack($order_num)
	{
		$order = (new OrderModel)->get(['order_num'=>$order_num]);

		if($order == null){
			return false;
		}

		//订单状态为5关闭，6取消时才有效
		if($order->order_status == 5 || $order->order_status == 6){
			//订单商品的列表
			$goods_list = (new OrderGoodsListModel)->all(['order_num'=>$order_num]);

			foreach ($goods_list as $key => $value) {
				//得到规格
				$spec = (new GoodsSpecListModel)->get(['sku'=>$value->order_goods_sku]);

				if($spec != null){
					//返还数量
					$spec->save(['num'=>$spec->num + $value->order_goods_num]);

					//返还商品总库存
					db('goods')->where(['id'=>$value->order_goods_id])->setInc('inventory',$value->order_goods_num);

				}

			}

			return true;

		}

		return false;

	}



	//支付成功回调

	public function payCallBack($order_num,$pay_type){



		$order_list = [];



		Db::startTrans();

		try{

			//判断大订单小订单

			if(strpos($order_num, 'member')){

				//大订单

				$member_order = ( new OrderMemberModel )->where(['order_member_num'=>$order_num])->find();

				if($member_order == null){

					Db::commit();

					return ['status'=>false,'message'=>'不存在该订单'];

				}

				if($member_order->pay == 1){

					Db::commit();

					return ['status'=>false,'message'=>'该订单已支付完成'];

				}

				if($member_order->pay == 2){

					Db::commit();

					return ['status'=>false,'message'=>'该订单已关闭，请重新创建订单'];

				}

				if($member_order->pay == 0){

					$order_list = json_decode($member_order->order_num);

				}

				$member_order->save(['pay'=>1]);

			}else{

				$order_list[] = $order_num;

			}



			//遍历订单列表

			foreach($order_list as $key => $value) {

				//订单
				$order = ( new OrderModel )->get(['order_num'=>$value]);

				if($order == null){
					Db::rollback();
					return ['status'=>false,'message'=>'存在错误的订单'.$value.'，请重试'];
				}

				if($order->order_status == 2){
					Db::rollback();
					return ['status'=>false,'message'=>'订单'.$value.'已支付，无法批量支付，请重试'];
				}

				if($order->order_status != 1){
					Db::rollback();
					return ['status'=>false,'message'=>'错误的订单状态'.$value.'，请重试'];
				}

		        //更改订单状态为已支付
				$order->order_status = 2;

				//添加订单支付时间
				$order->paytime = time();

				//订单支付方式
				if($pay_type != null){
					$order->money_type = $pay_type;
				}

				//退款时使用的订单号
				$order->pay_order = $order_num;

				//订单商品列表
				$goods_list = ( new OrderGoodsListModel )->field('order_goods_id,order_goods_num,order_goods_sku')->where(['order_num'=>$value])->select();
				foreach ($goods_list as $k => $v) {
					//销量增加
					db('goods_spec_list')->where(['sku'=>$v['order_goods_sku']])->setInc('sale_num',$v['order_goods_num']);

					db('goods')->where(['id'=>$v['order_goods_id']])->setInc('sell_num',$v['order_goods_num']);

				}

	        	$result = $order->save();

	        	if(!$result){

	        		Db::rollback();

	        		return ['status'=>false,'message'=>'订单修改失败'.$value.'，请重试'];

	        	}

	        	//---------------------------------------------//



	        	if($pay_type == 'balance'){

	        		$num = '301';

	        	}else{

	        		$num = '201';

	        	}



	        	//生成流水

	        	//顾客的财务账户

	        	$member_finance = ( new FinanceModel )->get(['finance_type_id'=>$order->member_id,'finance_type'=>'member']);

	        	//店铺的财务账户

	        	$store_finance = ( new FinanceModel )->get(['finance_type_id'=>$order->store_id,'finance_type'=>'store']);

	        	$data['remark'] = '订单支付';

	        	$data['money'] = $order->order_money;



	        	$data['member'] = $member_finance->finance_id;



	        	$data['store'] =  $store_finance->finance_id;

	        	( new FinanceAccountsModel )->behavior($num,$data);





	        	//----------------------------------------------//

			}

    		Db::commit();



    		return ['status'=>true,'message'=>'支付完成'];

        }catch(\Exception $e){

        	Db::rollback();

        	return ['status'=>false,'message'=>$e->getMessage()];

        }	

	

	}





	//传入订单号，返回订单信息

	public function returnOrderDetail($order_num){



		$order_list = [];



		//判断大订单小订单

		if(strpos($order_num, 'member')){

			//大订单

			$member_order = ( new OrderMemberModel )->where(['order_member_num'=>$order_num])->find();

			if($member_order == null){

				return ['status'=>false,'message'=>'不存在该订单'];

			}

			if($member_order->pay == 1){

				return ['status'=>false,'message'=>'该订单已支付完成'];

			}

			if($member_order->pay == 3){

				return ['status'=>false,'message'=>'该订单已关闭，请重新创建订单'];

			}

			// if($member_order->pay == 0){

				$order_list = json_decode($member_order->order_num);

			// }

		}else{

			$order_list[] = $order_num;

		}



		$return_list = [];

		foreach ($order_list as $key => $value) {

			//小订单

			$order = ( new OrderModel )->where(['order_num'=>$value])->find();

			if($order == null){

				return ['status'=>false,'message'=>'存在错误的订单，请重试'];

			}	

			$tmp['order_num'] = $order->order_num;

			$tmp['store_id'] = $order->store_id;

			$tmp['order_money'] = $order->order_money;

			$tmp['member_id'] = $order->member_id;

			$return_list[] = $tmp;

		}



		//创建流水



		return ['status'=>true,'message'=>'查询完成','data'=>$return_list];



	}

	//订单号，店铺id，金额，用户id



}